package win.smartown.mistyle;


import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.widget.FrameLayout;
import android.widget.TextView;

/**
 * Created by smartown on 2017/4/21 16:36.
 * Desc:
 */
public class OneButtonDialog extends DialogFragment {

    public static OneButtonDialog newInstance(@NonNull CharSequence title, @NonNull CharSequence buttonText, @LayoutRes int layoutId) {
        Bundle args = new Bundle();
        args.putCharSequence("title", title);
        args.putCharSequence("buttonText", buttonText);
        args.putInt("layoutId", layoutId);
        OneButtonDialog fragment = new OneButtonDialog();
        fragment.setArguments(args);
        return fragment;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Bundle bundle = getArguments();
        CharSequence title = "";
        CharSequence buttonText = "";
        int layoutId = 0;
        if (bundle != null) {
            title = bundle.getCharSequence("title");
            buttonText = bundle.getCharSequence("buttonText");
            layoutId = bundle.getInt("layoutId");
        }

        Dialog dialog = new Dialog(getActivity(), R.style.MiDialog);
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_one_button, null);
        TextView titleTextView = (TextView) view.findViewById(R.id.dialog_title);
        titleTextView.setText(title);

        ViewStub viewStub = (ViewStub) view.findViewById(R.id.dialog_content);
        viewStub.setLayoutResource(layoutId);
        if (callback != null) {
            callback.onContentViewCreated(viewStub.inflate());
        }

        TextView button = (TextView) view.findViewById(R.id.dialog_button);
        button.setText(buttonText);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (callback != null) {
                    callback.onButtonClick();
                }
            }
        });

        dialog.getWindow().setWindowAnimations(R.style.MiDialogAnimation);
        dialog.getWindow().setGravity(Gravity.BOTTOM);
        int margin = getResources().getDimensionPixelSize(R.dimen.dialog_window_margin);
        int screenWidth = Util.getScreenWidth(getActivity());
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(screenWidth - 2 * margin, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.bottomMargin = margin;
        dialog.setContentView(view, layoutParams);
        return dialog;
    }

    private Callback callback;

    public void setCallback(Callback callback) {
        this.callback = callback;
    }

    public interface Callback {

        public void onContentViewCreated(View view);

        public void onButtonClick();

    }

}
